package com.zryc.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zryc.service.IProductCategoryService;
import com.zryc.service.IProductInfoService;
import com.zryc.shop.entity.ProductCategory;
import com.zryc.shop.entity.ProductInfo;
import com.zryc.shop.utils.Result;
import io.seata.common.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 商品信息表 前端控制器
 * </p>
 *
 * @author xujiajun
 * @since 2022-10-17
 */
@RestController
@RequestMapping("/product")
@Api(tags = "商品信息模块接口说明")
public class ProductInfoController {
    @Autowired
    private IProductInfoService productInfoService;
    @Autowired
    private IProductCategoryService productCategoryService;
    /*
    keyword: null,
    pageNum: 1,
    pageSize: 5,
    publishStatus: null,
    verifyStatus: null,
    productSn: null,
    productCategoryId: null,
    brandId: null
     */
    @GetMapping("/list")
    @ApiOperation("查询商品列表")
    public Result list(@RequestParam(required = false) @ApiParam(value = "商品关键字") String keyword,
                       @RequestParam(required = false) @ApiParam(value = "商品上架状态") String publishStatus,
                       @RequestParam(required = false) @ApiParam(value = "审核状态") String verifyStatus,
                       @RequestParam(required = false) @ApiParam(value = "商品货号") String productSn,
                       @RequestParam(required = false) @ApiParam(value = "商品类别ID") Integer productCategoryId,
                       @RequestParam(required = false) @ApiParam(value = "品牌ID") Integer brandId,
                       @RequestParam(required = false,defaultValue = "0") @ApiParam(value = "商品的删除状态") Integer deleteStatus,
                       @RequestParam(required = true,defaultValue = "1") @ApiParam(value = "查询页号") Integer pageNum,
                       @RequestParam(required = true,defaultValue = "5") @ApiParam(value = "每页几条") Integer pageSize){
        QueryWrapper wrapper=new QueryWrapper();
        if (StringUtils.isNotBlank(keyword)){//当品牌名称条件不为空时，做like查询
            wrapper.like("product_name",keyword);
        }
        if (StringUtils.isNotBlank(publishStatus)){
            wrapper.eq("publish_status",publishStatus);
        }
        if (StringUtils.isNotBlank(verifyStatus)){
            wrapper.eq("audit_status",verifyStatus);
        }
        if (StringUtils.isNotBlank(productSn)){
            wrapper.eq("product_core",productSn);
        }
        if (productCategoryId!=null){
            wrapper.eq("two_category_id",productCategoryId);
        }
        if (brandId!=null){
            wrapper.eq("brand_id",brandId);
        }
        wrapper.eq("delete_status",deleteStatus);//设置查询商品的删除状态
        IPage<ProductInfo> page=productInfoService.page(new Page<ProductInfo>(pageNum,pageSize),wrapper);
        return new Result<>(true,200,"查询完成",page);
    }

    @PostMapping(path = "/create", consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation("添加商品")
    public Result create(@RequestBody ProductInfo productInfo){
        ProductCategory category=productCategoryService.getById(productInfo.getProductCategoryId());
        Short oneCategoryId = category.getParentId();
        Short twoCategoryId = productInfo.getProductCategoryId();
        try {
            productInfo.setOneCategoryId(oneCategoryId);
            productInfo.setTwoCategoryId(twoCategoryId);
            productInfoService.save(productInfo);
            return new Result(true,200,"添加商品成功",productInfo);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,500,"添加商品失败"+e.getMessage());
        }
    }

    @PostMapping(path = "/update/{pid}", consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation("修改商品信息")
    public Result update(@RequestBody ProductInfo productInfo,@PathVariable Integer pid){
        try {
            productInfo.setProductId(pid);
            productInfoService.updateById(productInfo);
            return new Result(true,200,"修改商品成功",productInfo);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,500,"修改商品失败"+e.getMessage());
        }
    }

    /**
     * 在修改之前需先查询商品详情
     * @param pid   商品ID
     * @return  商品信息
     */
    @GetMapping(path = "/updateInfo/{pid}")
    @ApiOperation("商品详情")
    public Result updateInfo(@ApiParam(value = "商品ID",required = true) @PathVariable Integer pid){
        ProductInfo productInfo=productInfoService.getById(pid);
        if (productInfo!=null){
            return new Result(true,200,"查询到该商品详情",productInfo);
        }else {
            return new Result(false,404,"该商品不存在");

        }
    }

    @PostMapping(path = "/update/publishStatus")
    @ApiOperation("更新商品上下架状态")
    public Result updatePublishStatus(@ApiParam(value = "商品ID",required = true) @RequestParam Integer[] ids,
                                      @ApiParam(value = "上架状态",required = true) @RequestParam Integer publishStatus) {
        UpdateWrapper wrapper = new UpdateWrapper();
        wrapper.in("product_id",ids);
        wrapper.set("publish_status", publishStatus);
        if (productInfoService.update(wrapper)) {
            return new Result(true, 200,publishStatus==1? "商品已上架":"商品已下架");
        } else {
            return new Result(false, 500, "修改商品上架状态失败");
        }
    }
    @PostMapping(path = "/update/deleteStatus")
    @ApiOperation("更新商品删除状态")
    public Result deleteStatus(@ApiParam(value = "商品ID",required = true) @RequestParam Integer[] ids,
                               @ApiParam(value = "删除状态",required = true) @RequestParam Integer deleteStatus){
            UpdateWrapper wrapper=new UpdateWrapper();
//            List ids= (List) params.get("ids");
            wrapper.in("product_id",ids);
            wrapper.set("delete_status",deleteStatus);
            if (productInfoService.update(wrapper)){
                return new Result(true,200,"删除成功");
            }else{
                return new Result(false,500,"删除失败");
            }
        }







}
